#include "pch.h"
#include "TextFormat.h"
#include "Helper.h"
#include "ErrorCodes.h"
#include "Factories.h"

using namespace Platform;
using namespace Microsoft::WRL;

using namespace DXBase;
using namespace DXBase::DWrite;


TextFormat::TextFormat()
{
	fontFamilyName = L"Courier";
	localeName = L"en-US";
	fontSize = 12;
	fontWeight = DWRITE_FONT_WEIGHT_NORMAL;
	fontStyle = DWRITE_FONT_STYLE_NORMAL;
	fontStretch = DWRITE_FONT_STRETCH_NORMAL;

	flowDirection = DWRITE_FLOW_DIRECTION_TOP_TO_BOTTOM;
	incrementalTabStop = -1;
	paragraphAlignment = DWRITE_PARAGRAPH_ALIGNMENT_NEAR;
	readingDirection = DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
	textAlignment = DWRITE_TEXT_ALIGNMENT_LEADING;
	wordWrapping = DWRITE_WORD_WRAPPING_NO_WRAP;
}

Microsoft::WRL::ComPtr<IDWriteTextFormat> TextFormat::GetNativeFormat()
{
	if (!mTextFmt.Get())
	{
		// TODO IDWriteCollection
		HR2Ex(Factories::GetDWriteFactory()->CreateTextFormat(
			fontFamilyName->Data(),
			NULL,
			fontWeight,
			fontStyle,
			fontStretch,
			fontSize,
			localeName->Data(),
			mTextFmt.GetAddressOf()
        ));
		if (FAILED(mTextFmt->SetFlowDirection(flowDirection)))
			flowDirection = DWRITE_FLOW_DIRECTION_TOP_TO_BOTTOM;
		if (incrementalTabStop < 0 || FAILED(mTextFmt->SetIncrementalTabStop(incrementalTabStop)))
			incrementalTabStop = mTextFmt->GetIncrementalTabStop();
		if (FAILED(mTextFmt->SetParagraphAlignment(paragraphAlignment)))
			paragraphAlignment = mTextFmt->GetParagraphAlignment();
		if (FAILED(mTextFmt->SetReadingDirection(readingDirection)))
			readingDirection = mTextFmt->GetReadingDirection();
		if (FAILED(mTextFmt->SetTextAlignment(textAlignment)))
			textAlignment = mTextFmt->GetTextAlignment();
		if (FAILED(mTextFmt->SetWordWrapping(wordWrapping)))
			wordWrapping = mTextFmt->GetWordWrapping();
	}
	return mTextFmt;
}
